package study.source.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;




public class ExcelReaderPoi {
	
	private XSSFWorkbook workbook = null;
	XSSFSheet sheet = null;
	XSSFRow row = null;
	ArrayList<String[]> data = null;
	String[] dataSt = null;
	
	public ExcelReaderPoi(){
		
	}
	
	public ExcelReaderPoi(File excelFile) throws FileNotFoundException, IOException{
		this.workbook = new XSSFWorkbook(new FileInputStream(excelFile));
	}
	
	public List<String[]> excelRead() {
		data = new ArrayList<String[]>();
		
		for(int i = 0; i < workbook.getNumberOfSheets() ; i++ )
		{
			XSSFSheet sheet = workbook.getSheetAt(i);
			for(int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++)
			{
				row = sheet.getRow(j);
				if(row!=null)
				{
					dataSt = new String[row.getLastCellNum()+1];
					for(int k = 0; k<= row.getLastCellNum(); k++)
					{							
						XSSFCell cell = row.getCell(k);
						if(cell!=null)
						{							
							int cellType = cell.getCellType();
							if(cellType == XSSFCell.CELL_TYPE_NUMERIC)
							{
								dataSt[k] = String.valueOf((int)cell.getNumericCellValue());
							}else if(cellType == XSSFCell.CELL_TYPE_STRING)
							{
								dataSt[k] = cell.getStringCellValue();									
							}
							
						}
						System.out.print(dataSt[k]+"\t");
					}
					data.add(dataSt);
					System.out.println();
				}
			}
		}
		
		
		/*for(int i=0; i<workbook.getNumberOfSheets(); i++){
			sheet = workbook.getSheetAt(i);
			dataSt = new String[sheet.getLastRowNum()];
			for(int j=0; j<sheet.getPhysicalNumberOfRows(); j++){
				XSSFCell cell = null;
				if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
					dataSt[j] = String.valueOf(cell.getNumericCellValue());
				}else{
					dataSt[j] = cell.getStringCellValue();
				}
			}
			data.add(dataSt);
		}*/
		
		return data;
	}
	
	
	
}
